package com.bw.interceptor;

import com.bw.utils.auth.JwtUtils;
import com.bw.utils.auth.RsaUtils;
import com.bw.utils.auth.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.PublicKey;

public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		//获取公钥
		String token = request.getHeader("token");
		if(StringUtils.isEmpty(token)){
			return false;
		}

		try{
			//获取公钥
			PublicKey publicKey = RsaUtils.getPublicKey("D:\\1711A\\keys\\rea.pub");
			//根据公钥解密
			UserInfo userInfo  = JwtUtils.getInfoFromToken(token,publicKey);
			if(userInfo!=null){
				return true;
			}
		}catch (Exception e){
			e.printStackTrace();
			return false;
		}
		return false;
	}
}
